package p.ithorns.framework.common.utils;


import p.ithorns.framework.common.model.ParameterizedTypeImpl;

import java.lang.reflect.Type;

/**
 * JsonAssist
 * Fastjson辅助工具类
 * 用法：
 * - JSON.parseObject(json, JsonAssist.getNestedType(types))
 * - JSONObject.toJavaObject(JsonAssist.getNestedType(R.class, T.class));
 *
 * @author HtL
 * @date 2024/10/21 13:48
 * @since 1.0.0
 */
public class JsonAssist {

    /**
     * 构建嵌套泛型类型
     * 从外部类到内部类开始构建
     *
     * @param types -泛型类型
     * @return Type
     */
    public static Type getNestedType(Type... types) {
        // ParameterizedTypeImpl inner = new ParameterizedTypeImpl(new Type[]{rawClazz}, null, List.class);
        // ParameterizedTypeImpl outer = new ParameterizedTypeImpl(new Type[]{inner}, null, outClazz);
        ParameterizedTypeImpl beforeType = null;
        if (types != null && types.length > 0) {
            for (int i = types.length - 1; i > 0; i--) {
                beforeType = new ParameterizedTypeImpl(new Type[]{beforeType == null ? types[i] : beforeType}, null, types[i - 1]);
            }
        }
        return beforeType;
    }

}